<template>
  <div>
    <Drawer
      ref="modal"
      :title="title"
      :width="editType != 'settingAdmin'?'700':'1000'"
      :value="show"
      :beforeClose="$_modal_decideIfClose"
    >
      <Form v-if="editType != 'settingAdmin'"
        ref="form"
        :labelWidth="130"
        :model="formData"
        :rules="formRule"
      >
        <FormItem prop="areaName" :label="$t('newDevices.siteName')">
          <Input style="width: 300px" v-model="formData.areaName" :placeholder="$t('newDevices.siteNameTip')" />
        </FormItem>
        <FormItem prop="recordTypes" :label="$t('newDevices.siteParent')">
          
          <Cascader style="width: 300px" :data="recordTypes" v-model="recordType" disabled 
            @on-change="handleChange1"
            @on-visible-change="handleChange" change-on-select>
          </Cascader>
          <div v-show="formData.areaType==1" style="line-height:20px;margin-top:8px">{{$t('newDevices.siteParentTip')}}</div>
        </FormItem>
        <!-- <FormItem prop="sort" :label="$t('newDevices.sort')">
          <InputNumber
            v-model="formData.sort"
            :min="0"
            :step="1"
            :style="{ width: '100px' }"
          />
        </FormItem> -->
        <FormItem prop="areaType" :label="$t('newDevices.siteType')" v-if="editType !='update'">
          <RadioGroup v-model="formData.areaType">
              <Radio label="0" >
                <span>{{ $t("newDevices.public") }}</span>
              </Radio>
              <Radio label="1" >
                <span>{{ $t("newDevices.independence") }}</span>
              </Radio>
          </RadioGroup>
        </FormItem>
        <FormItem prop="areaType" :label="$t('newDevices.siteType')" v-if="editType =='update'">
          <span v-if="formData.areaType==0">{{ $t("newDevices.public") }}</span>
          <span v-if="formData.areaType==1">{{ $t("newDevices.independence") }}</span>
        </FormItem>
        <FormItem >
          <Card style="width:500px" v-show="formData.areaType=='0'">
              <p slot="title">{{$t('newDevices.typeDec')}}:{{$t('newDevices.public')}}</p>
              <p>{{$t('newDevices.publicDec')}}</i></p>
              <p>{{$t('newDevices.publicDec1')}}</i></p>
          </Card>
          <Card style="width:500px" v-show="formData.areaType=='1'">
              <p slot="title">{{$t('newDevices.typeDec')}}:{{$t('newDevices.independence')}}</p>
              <p>{{$t('newDevices.independenceDec')}}</i></p>
              <p>{{$t('newDevices.independenceDec1')}}</i></p>
          </Card>
        </FormItem>
        <FormItem class="glo-btn-group">
          <Button
            type="primary"
            :loading="loading"
            @click="handleSubmit"
          >{{ $t("devicesCreate.devCreateSubmit") }}</Button>
          <Button
            @click="$_modal_close"
          >{{ $t("devicesCreate.devCreateCancel") }}</Button>
        </FormItem>
      </Form>
      <Form v-if="editType == 'settingAdmin'"
        :labelWidth="130"
        :model="formData1"
        :rules="formRule"
      >
        <FormItem prop="recordTypes" :label="$t('newDevices.site')">
          <!-- <Cascader style="width: 300px" :data="recordTypes" v-model="recordType"
            @on-change="handleChange1"
            @on-visible-change="handleChange" change-on-select>
          </Cascader> -->
          {{editItem.areaName}}
        </FormItem>
        <FormItem prop="administrator" :label="$t('companyManagement.administrator')"> 
          <Form ref="formDynamic"  :model="formDynamic" :label-width="80" style="" >
            <FormItem
                  style="margin-bottom: 26px"
                  v-for="(item, index) in formDynamic.items"
                  v-if="item.status"
                  :key="index"
                  :prop="'items.' + index + '.value'"
                  :rules="userRule"
                  ref="formAdmin"
                  >
              <Row>
                  <Col span="5">
                      <Input style="width: 200px"  v-model.trim="item.value.username" :placeholder="$t('permissionsIndex.UserName')" clearable>
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                      </Input>
                  </Col>
                  <Col span="5" offset="1">
                      <Input style="width: 200px" type="password" v-model.trim="item.value.password" :placeholder="$t('permissionsIndex.UserInputPass')" clearable>
                        <Icon type="ios-lock-outline" slot="prepend"></Icon>
                      </Input>
                  </Col>
                  <Col span="5" offset="1">
                      <Input style="width: 200px" type="password" v-model.trim="item.value.passwordSure" :placeholder="$t('permissionsIndex.UserInputSelPass')" clearable>
                        <Icon type="ios-lock-outline" slot="prepend"></Icon>
                      </Input>
                  </Col>
                  <Col span="4" offset="1">
                    <Button @click="handleRemove(index)" >{{$t('emailSetting.Del')}}</Button>                  
                  </Col>
              </Row>
            </FormItem>
            <FormItem>
                <Row>
                    <Col span="4">
                        <Button type="dashed" long @click="handleAdd" icon="md-add">{{$t('emailSetting.Add')}}</Button>
                    </Col>
                </Row>
            </FormItem>           
          </Form>
        </FormItem>
        <!-- <FormItem prop="roles" :label="$t('permissionsIndex.UserRoleSel')">
          <CheckboxGroup v-model="roles">
              <Checkbox v-for="(item, idx) in roleCheck" :key="idx" :value="item.id" :label="item.id">{{item.roleName}}</Checkbox>
          </CheckboxGroup>
        </FormItem> -->
        <!-- <FormItem prop="dataPermissData" :label="$t('permissionsIndex.rolepermissionsSel')"> -->
        <FormItem prop="dataPermiss" :label="$t('permissionsIndex.rolepermissionsSel')">
          <Tree 
            style="margin-top:-6px"
            @on-check-change ='TreeChange'
            ref="tree"
             :data="dataPermiss" show-checkbox>
          </Tree>
        </FormItem>
        <FormItem prop="dateTime" :label="$t('newDevices.dateTime')">
          <RadioGroup v-model="dateTime">
            <Radio label="1">
              <DatePicker
                class="date-widget"
                v-model="selectDate"
                type="daterange"
                :placeholder="$t('emailSetting.BodyTemAnimalTimeScope')"
              />
            </Radio>
            <Radio label="0">
              <span>{{ $t("newDevices.longTime") }}</span>
            </Radio>
          </RadioGroup>
        </FormItem>
        <FormItem class="glo-btn-group">
          <Button
            type="primary"
            :loading="loading"
            @click="settIngAdmin"
          >{{ $t("devicesCreate.devCreateSubmit") }}</Button>
          <Button
            @click="$_modal_close"
          >{{ $t("devicesCreate.devCreateCancel") }}</Button>
        </FormItem>
      </Form>
    </Drawer>
  </div>
</template>

<script>
import _ from 'lodash'
import moment from 'moment'
import {
  getSite,
  getParentArea,
  getAreaItem,
  addAreaAdmin,
  updateArea,
  addArea
} from '@/api/newDevices.js'
import {
  getList,
} from '@/api/Role.js'
import modalMixin from '@/utils/mixins/modalMixin.js'
// import {
//   create,
// } from '@/api/user.js'

const DEFAULT_FORM = {
  areaName: '',
  departOrder: 0,
  areaType: '0',//公共0
  // sort: 1,
  jurisdiction:[],
  administrator:''
}

export default {
  mixins: [
    modalMixin,
  ],

  props: {
    editType: {
      type: String,
      default: 'create',
    },
    editItem: Object,
    parentDepart: [String, Number],
  },
  data () {
    return {
      // 动态接收邮箱表单数据
      index: 1,
      formDynamic: {
        items: [
          {
            value: {
              username:'',
              password:'',
              passwordSure:'',
            },//名字
            index: 1,
            // password:'',
            // passwordSure:'',
            status: 1
          }
        ]
      },
      dateTime:'0',//0永久
      selectDate: [],
      title:'',
      recordTypes: [],

      recordType: [],
      // roleCheck: [],
      // roles: [],
      // jurisdictionTypes:[
      //   {
      //     label: this.$t('newDevices.jurisdiction1'),
      //     value: '1',
      //   },
      //   {
      //     label: this.$t('newDevices.jurisdiction2'),
      //     value: '2',
      //   },
      //   {
      //     label: this.$t('newDevices.jurisdiction3'),
      //     value: '3',
      //   },
      //   {
      //     label: this.$t('newDevices.jurisdiction4'),
      //     value: '4',
      //   },
      //   {
      //     label: this.$t('newDevices.jurisdiction5'),
      //     value: '5',
      //   },
      //   {
      //     label: this.$t('newDevices.jurisdiction6'),
      //     value: '6',
      //   },
      // ],
      formData: _.cloneDeep(DEFAULT_FORM),
      formData1:{},
      formRule: {
        areaName: { required: true, message: this.$t('emailSetting.Noempty') },
        dateTime: { required: true, validator: (rule, value, callback) => {
          if (!this.selectDate[0]&&this.dateTime==1) {
            return callback(new Error(this.$t('devicesCreate.selectTime')))
          } else {
            return callback()
          }
        } },
        // dataPermissData: { required: true, validator: (rule, value, callback) => {
        //   console.log('dataPermiss',value)
        //   console.log('dataPermiss',this.dataPermissData)
        //   // if (!value) {
        //   if (!this.dataPermissData[0]) {
        //     return callback(new Error(this.$t('permissionsIndex.roleSelper')))
        //   } else {
        //     return callback()
        //   }
        // } },
      },
      userRule: 
        [
          // {required: true, message: this.$t('emailSetting.Noempty'), trigger: 'blur'},
          {validator: (rule, value, callback) => {
          if (!value) {
            return callback(new Error(this.$t('emailSetting.Noempty')))
          } else {
            if(!value.username){
              return callback(new Error(this.$t('loginIndex.loginPhoneHint')))
            }
            else if(!value.password){
              return callback(new Error(this.$t('loginIndex.loginPassInput')))
            }
            else if(value.password.length<6){
              return callback(new Error(this.$t('loginIndex.loginPhoneMin')))
            }
            else if(!value.passwordSure){
              return callback(new Error(this.$t('loginIndex.loginpwdCheck1')))
            }
            else if(value.passwordSure!=value.password){
              return callback(new Error(this.$t('loginIndex.loginpwdCheck2')))
            }else{
              return callback()
            }
            
          }
        } , trigger: 'blur'},   
      ],
      dataPermiss: [
        {
          id:56,
          parentId:0,
          title: this.$t('navigationLeft.newDevices'),
          sort: 0,
          expand: false,
          permission: '#newDevices',
          children: [
            {
              id:57,
              parentId:56,
              title: this.$t('newDevices.site'),
              sort: 1,
              expand: true,
              permission: '#site',
              children: [
                {
                  id:58,
                  parentId:57,
                  title: this.$t('newDevices.settingAdmin'),
                  sort: 1,
                  expand: true,
                  permission: '#settingAdmin',
                },
                {
                  id:59,
                  parentId:57,
                  title: this.$t('newDevices.creatSite'),
                  sort: 2,
                  expand: true,
                  permission: '#creatSite',
                },
                {
                  id:60,
                  parentId:57,
                  title: this.$t('staffIndex.staffDepartmentAddChildEdit'),
                  sort: 3,
                  expand: true,
                  permission: '#staffDepartmentAddChildEdit',
                },
                {
                  id:61,
                  parentId:57,
                  title: this.$t('staffIndex.staffDepartmentAddChildDel'),
                  sort: 4,
                  expand: true,
                  permission: '#staffDepartmentAddChildDel',
                },
              ],
            },
            {
              id:62,
              parentId:56,
              title: this.$t('companyManagement.administrator'),
              sort: 2,
              expand: true,
              permission: '#administrator',
              // children: [
              //   {
              //     id:63,
              //     parentId:62,
              //     title: this.$t('versionIndex.verListSettingsEdit'),
              //     sort: 1,
              //     expand: true,
              //     permission: '#verListSettingsEdit7',
              //   },
              //   {
              //     id:64,
              //     parentId:62,
              //     title: this.$t('versionIndex.verListSettingsDel'),
              //     sort: 2,
              //     expand: true,
              //     permission: '#verListSettingsDel7',
              //   },
              // ],
            },
            {
              id:65,
              parentId:56,
              title: this.$t('staffIndex.staffDepartCrewList'),
              sort: 3,
              expand: true,
              permission: '#staffDepartCrewList',
              children: [
                {
                  id:66,
                  parentId:65,
                  title: this.$t('versionIndex.verAddBtn'),
                  sort: 1,
                  expand: true,
                  permission: '#verAddBtn2',
                },
                {
                  id:67,
                  parentId:65,
                  title: this.$t('staffIndex.stafferrorDel'),
                  sort: 2,
                  expand: true,
                  permission: '#stafferrorDel4',
                },
                {
                  id:68,
                  parentId:65,
                  title: this.$t('newDevices.jurisdiction'),
                  sort: 3,
                  expand: true,
                  permission: '#verListSettingsEdit8',
                },
                {
                  id:69,
                  parentId:65,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 4,
                  expand: true,
                  permission: '#verListSettingsDel8',
                },
              ],
            },
            {
              id:77,
              parentId:56,
              title: this.$t('devicesIndex.devStrEquipment'),
              sort: 4,
              expand: true,
              permission: '#devList',
              children: [
                {
                  id:78,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListEdit'),
                  sort: 1,
                  expand: true,
                  permission: '#devListEdit3',
                },
                {
                  id:79,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListRelieve'),
                  sort: 2,
                  expand: true,
                  permission: '#devListRelieve2',
                },
                {
                  id:80,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListCut'),
                  sort: 3,
                  expand: true,
                  permission: '#devListCut2',
                },
                {
                  id:81,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListrestart'),
                  sort: 4,
                  expand: true,
                  permission: '#devListrestart2',
                },
                {
                  id:82,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListDel'),
                  sort: 5,
                  expand: true,
                  permission: '#devListDel2',
                },
                {
                  id:83,
                  parentId:77,
                  title: this.$t('devicesCreate.devTimezones'),
                  sort: 6,
                  expand: true,
                  permission: '#devTimezones2',
                },
                {
                  id:84,
                  parentId:77,
                  title: this.$t('newDevices.addEquipments'),
                  sort: 7,
                  expand: true,
                  permission: '#addEquipments',
                },
                {
                  id:86,
                  parentId:77,
                  title: this.$t('devicesIndex.restoreSetting'),
                  sort: 8,
                  expand: true,
                  permission: '#restoreSetting1',
                },
              ],
            },
          ],
        },
        {
          id:1,
          parentId:0,
          title: this.$t('navigationLeft.equipment'),
          sort: 1,
          expand: false,
          permission: '#device',
          children: [
            {
              id:2,
              parentId:1,
              title: this.$t('devicesListSettings.devListEdit'),
              sort: 1,
              expand: true,
              permission: '#devListEdit',
            },
            // {
            //   id:3,
            //   parentId:1,
            //   title: this.$t('devicesListSettings.devListRelieve'),
            //   sort: 2,
            //   expand: true,
            //   permission: '#devListRelieve',
            // },
            {
              id:4,
              parentId:1,
              title: this.$t('devicesListSettings.devListCut'),
              sort: 3,
              expand: true,
              permission: '#devListCut',
            },
            {
              id:5,
              parentId:1,
              title: this.$t('devicesListSettings.devListrestart'),
              sort: 4,
              expand: true,
              permission: '#devListrestart',
            },
            {
              id:6,
              parentId:1,
              title: this.$t('devicesListSettings.devListDel'),
              sort: 5,
              expand: true,
              permission: '#devListDel',
            },
            {
              id:7,
              parentId:1,
              title: this.$t('devicesCreate.devTimezones'),
              sort: 6,
              expand: true,
              permission: '#devTimezones',
            },
            {
              id:8,
              parentId:1,
              title: this.$t('devicesIndex.devOfflineHint'),
              sort: 7,
              expand: true,
              permission: '#devOfflineHint',
            },
            {
              id:9,
              parentId:1,
              title: this.$t('devicesIndex.devUpgradeBtn'),
              sort: 8,
              expand: true,
              permission: '#devUpgradeBtn',
            },
            {
              id:10,
              parentId:1,
              title: this.$t('devicesIndex.restoreSetting'),
              sort: 9,
              expand: true,
              permission: '#restoreSetting',
            },
          ],
        },
        {
          id:11,
          parentId:0,
          title: this.$t('navigationLeft.staff'),
          sort: 2,
          expand: false,
          permission: '#staff',
          children: [
            // {
            //   title: this.$t('AccessIndex.AccessJobdeNum'),
            //   sort: 1,
            //   expand: true,
            //   permission: '#AccessJobdeNum',
            //   children: [
            //     {
            //       title: this.$t('AccessIndex.AccessJobdeNum'),
            //       sort: 1,
            //       expand: true,
            //       permission: '#AccessJobdeNum',
            //     },
            //     {
            //       title: this.$t('staffIndex.staffSuccessList'),
            //       sort: 2,
            //       expand: true,
            //       permission: '#staffSuccessList',
            //     },
            //     {
            //       title: this.$t('staffIndex.stafferrorList'),
            //       sort: 3,
            //       expand: true,
            //       permission: '#stafferrorList',
            //     },
            //   ],
            // },
            {
              id:12,
              parentId:11,
              title: this.$t('staffIndex.staffSuccessList'),
              sort: 2,
              expand: true,
              permission: '#staffSuccessList1',
              children: [
                {
                  id:13,
                  parentId:12,
                  title: this.$t('staffIndex.staffTransfer'),
                  sort: 1,
                  expand: true,
                  permission: '#staffTransfer',
                },
                {
                  id:14,
                  parentId:12,
                  title: this.$t('staffIndex.staffBatchUpload'),
                  sort: 2,
                  expand: true,
                  permission: '#staffBatchUpload',
                },
                {
                  id:15,
                  parentId:12,
                  title: this.$t('staffIndex.stafferrorDel'),
                  sort: 3,
                  expand: true,
                  permission: '#stafferrorDel',
                },
                {
                  id:16,
                  parentId:12,
                  title: this.$t('versionIndex.verListSettingsEdit'),
                  sort: 4,
                  expand: true,
                  permission: '#verListSettingsEdit',
                },
                {
                  id:17,
                  parentId:12,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 5,
                  expand: true,
                  permission: '#verListSettingsDel',
                },
                {
                  id:18,
                  parentId:12,
                  title: this.$t('staffIndex.staffeIDcardBtn'),
                  sort: 6,
                  expand: true,
                  permission: '#staffeIDcardBtn',
                },
                {
                  id:19,
                  parentId:12,
                  title: this.$t('staffIndex.staffDepartCreateAdd'),
                  sort: 7,
                  expand: true,
                  permission: '#staffDepartCreateAdd',
                },
                {
                  id:20,
                  parentId:12,
                  title: this.$t('staffIndex.staffclickToDow'),
                  sort: 8,
                  expand: true,
                  permission: '#staffclickToDow',
                },
                {
                  id:21,
                  parentId:12,
                  title: this.$t('staffIndex.staffclickToDowIDCard'),
                  sort: 9,
                  expand: true,
                  permission: '#staffclickToDowIDCard',
                },
              ],
            },
            {
              id:22,
              parentId:11,
              title: this.$t('staffIndex.stafferrorList'),
              sort: 3,
              expand: true,
              permission: '#stafferrorList1',
              children: [
                {
                  id:23,
                  parentId:22,
                  title: this.$t('staffIndex.staffFailureLog'),
                  sort: 1,
                  expand: true,
                  permission: '#staffFailureLog',
                },
                {
                  id:24,
                  parentId:22,
                  title: this.$t('staffIndex.stafferrorDel'),
                  sort: 2,
                  expand: true,
                  permission: '#stafferrorDel2',
                },
                {
                  id:25,
                  parentId:22,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 3,
                  expand: true,
                  permission: '#verListSettingsDel1',
                },
              ],
            },
          ],
        },
        // {
        //   id:26,
        //   parentId:0,
        //   title: this.$t('navigationLeft.area'),
        //   sort: 3,
        //   expand: false,
        //   permission: '#area',
        //   children: [
        //     {
        //       id:27,
        //       parentId:26,
        //       title: this.$t('areaIndex.areaAddList'),
        //       sort: 1,
        //       expand: true,
        //       permission: '#areaAddList',
        //     },
        //     {
        //       id:28,
        //       parentId:26,
        //       title: this.$t('versionIndex.verListSettingsEdit'),
        //       sort: 2,
        //       expand: true,
        //       permission: '#verListSettingsEdit2',
        //     },
        //     {
        //       id:29,
        //       parentId:26,
        //       title: this.$t('versionIndex.verListSettingsDel'),
        //       sort: 3,
        //       expand: true,
        //       permission: '#verListSettingsDel2',
        //     },
            
        //   ],
        // },
        
        // {
        //   id:30,
        //   parentId:0,
        //   title: this.$t('Attendance.AttendanceManage'),
        //   sort: 4,
        //   expand: false,
        //   permission: '#attendance',
        //   children: [
        //     {
        //       id:73,
        //       parentId:30,
        //       title: this.$t('Attendance.AttendanceAdd'),
        //       sort: 1,
        //       expand: true,
        //       permission: '#AttendanceAdd',
        //     },
        //     {
        //       id:74,
        //       parentId:30,
        //       title: this.$t('versionIndex.verListSettingsEdit'),
        //       sort: 2,
        //       expand: true,
        //       permission: '#verListSettingsEdit3',
        //     },
        //     {
        //       id:75,
        //       parentId:30,
        //       title: this.$t('versionIndex.verListSettingsDel'),
        //       sort: 3,
        //       expand: true,
        //       permission: '#verListSettingsDel3',
        //     },
        //     {
        //       id:76,
        //       parentId:30,
        //       title: this.$t('Attendance.AttendanceExport'),
        //       sort: 4,
        //       expand: true,
        //       permission: '#AttendanceExport',
        //     },
        //   ],
        // },
        {
          id:31,
          parentId:0,
          title: this.$t('navigationLeft.visitors'),
          sort: 5,
          expand: false,
          permission: '#visitors',
          children: [
            {
              id:32,
              parentId:31,
              title: this.$t('visitorsIndex.visitorsAdd'),
              sort: 1,
              expand: true,
              permission: '#visitorsAdd',
            },
            {
              id:33,
              parentId:31,
              title: this.$t('staffIndex.stafferrorDel'),
              sort: 2,
              expand: true,
              permission: '#stafferrorDel3',
            },
            {
              id:34,
              parentId:31,
              title: this.$t('devicesListSettings.devListEdit'),
              sort: 3,
              expand: true,
              permission: '#devListEdit2',
            },
            {
              id:35,
              parentId:31,
              title: this.$t('devicesListSettings.devListDel'),
              sort: 4,
              expand: true,
              permission: '#devListDel1',
            }
          ],
        },
        {
          id:36,
          parentId:0,
          title: this.$t('navigationLeft.bodyTemperature'),
          sort: 6,
          expand: false,
          permission: '#temperature',
          children: [
            {
              id:37,
              parentId:36,
              title: this.$t('BodyTemperatureIndex.BodyTemAnimalRecord'),
              sort: 1,
              expand: true,
              permission: '#BodyTemAnimalRecord',
            },
          ],
        },
        {
          id:38,
          parentId:0,
          title: this.$t('navigationLeft.accessRecord'),
          sort: 7,
          expand: false,
          permission: '#accessRecord',
          children: [
            {
              id:85,
              parentId:38,
              title: this.$t('staffIndex.staffFailureLog'),
              sort: 1,
              expand: true,
              permission: '#staffFailureLog1',
            },
          ],
        },
        {
          id:39,
          parentId:0,
          title: this.$t('UlogI18n.UlogI18nTit'),
          sort: 8,
          expand: false,
          permission: '#uLog',
          children: [
            {
              id:40,
              parentId:39,
              title: this.$t('staffIndex.staffFailureLog'),
              sort: 1,
              expand: true,
              permission: '#staffFailureLog2',
            },
          ],
        },
        // {
        //   id:41,
        //   parentId:0,
        //   title: this.$t('navigationLeft.permissions'),
        //   sort: 9,
        //   expand: false,
        //   permission: '#permissions',
        //   children: [
        //     {
        //       id:42,
        //       parentId:41,
        //       title: this.$t('permissionsIndex.UserTit'),
        //       sort: 1,
        //       expand: true,
        //       permission: '#UserTit',
        //       children: [
        //         {
        //           id:43,
        //           parentId:42,
        //           title: this.$t('versionIndex.verAddBtn'),
        //           sort: 1,
        //           expand: true,
        //           permission: '#verAddBtn',
        //         },
        //         {
        //           id:44,
        //           parentId:42,
        //           title: this.$t('versionIndex.verListSettingsEdit'),
        //           sort: 2,
        //           expand: true,
        //           permission: '#verListSettingsEdit4',
        //         },
        //         {
        //           id:45,
        //           parentId:42,
        //           title: this.$t('versionIndex.verListSettingsDel'),
        //           sort: 3,
        //           expand: true,
        //           permission: '#verListSettingsDel4',
        //         },
        //         {
        //           id:46,
        //           parentId:42,
        //           title: this.$t('loginIndex.loginPhoneEdit'),
        //           sort: 4,
        //           expand: true,
        //           permission: '#loginPhoneEdit',
        //         },
        //       ],
        //     },
        //     {
        //       id:47,
        //       parentId:41,
        //       title: this.$t('permissionsIndex.roleTitle'),
        //       sort: 2,
        //       expand: true,
        //       permission: '#roleTitle',
        //       children: [
        //         {
        //           id:48,
        //           parentId:47,
        //           title: this.$t('permissionsIndex.roleAdd'),
        //           sort: 1,
        //           expand: true,
        //           permission: '#roleAdd',
        //         },
        //         {
        //           id:49,
        //           parentId:47,
        //           title: this.$t('versionIndex.verListSettingsEdit'),
        //           sort: 2,
        //           expand: true,
        //           permission: '#verListSettingsEdit5',
        //         },
        //         {
        //           id:50,
        //           parentId:47,
        //           title: this.$t('versionIndex.verListSettingsDel'),
        //           sort: 3,
        //           expand: true,
        //           permission: '#verListSettingsDel5',
        //         },
        //       ],
        //     },
        //   ],
        // },
        // {
        //   id:51,
        //   parentId:0,
        //   title: this.$t('navigationLeft.TerminalVersion'),
        //   sort: 10,
        //   expand: false,
        //   permission: '#version',
        //   children: [
        //     {
        //       id:70,
        //       parentId:51,
        //       title: this.$t('versionIndex.verAddBtn'),
        //       sort: 1,
        //       expand: true,
        //       permission: '#verAddBtn1',
        //     },
        //     {
        //       id:71,
        //       parentId:51,
        //       title: this.$t('versionIndex.verListSettingsEdit'),
        //       sort: 2,
        //       expand: true,
        //       permission: '#verListSettingsEdit6',
        //     },
        //     {
        //       id:72,
        //       parentId:51,
        //       title: this.$t('versionIndex.verListSettingsDel'),
        //       sort: 3,
        //       expand: true,
        //       permission: '#verListSettingsDel6',
        //     },
        //   ],
        // },
        {
          id:52,
          parentId:0,
          title: this.$t('navigationLeft.emailAdmin'),
          sort: 11,
          expand: false,
          permission: '#emailAdmin',
          children: [
            {
              id:53,
              parentId:52,
              title: this.$t('emailSetting.setting'),
              sort: 1,
              expand: true,
              permission: '#setting',
            },
          ],
        },
        // {
        //   id:54,
        //   parentId:0,
        //   title: this.$t('navigationLeft.companyManagement'),
        //   sort: 12,
        //   expand: false,
        //   permission: '#companyManagement',
        //   children: [],
        // },
        // {
        //   id:55,
        //   parentId:0,
        //   title: this.$t('navigationLeft.warningMessage'),
        //   sort: 13,
        //   expand: false,
        //   permission: '#warningMessage',
        //   children: [],
        // },
        {
          id:87,
          parentId:0,
          title: this.$t('navigationLeft.advertising'),
          sort: 13,
          expand: false,
          permission: '#advertising',
          children: [],
        },
      ],
      
      dataPermissData: [],
      // hideDel:false,
    }
  },

  mounted () {
    this.show = true
    this.getSiteList()
    if (this.editType === 'update') {
      const formData = {}
      const keys = Object.keys(DEFAULT_FORM)
      keys.forEach(key => {
        formData[key] = _.isUndefined(this.editItem[key])
          ? DEFAULT_FORM[key]
          : this.editItem[key]
      })
      formData.id = this.editItem.id
      this.formData = _.cloneDeep(formData)
      if(this.formData.areaType=='G'){
        this.formData.areaType='0'
      }else if(this.formData.areaType=='D'){
        this.formData.areaType='1'
      }
      
      this.title=this.$t('newDevices.updateSite')
      this.getParent()
    }else if(this.editType === 'create'){
      this.title=this.$t('newDevices.creatSite')
      this.getParent()
    }else if(this.editType === 'settingAdmin'){
      this.title=this.$t('newDevices.settingAdmin')
      // console.log('this.editItem133')
      // console.log(this.editItem)
      // this.getAreaItem1()
    }
    
  },

  methods: {
    // TreeChange () {
    //   this.dataPermissData = this.$refs.tree.getCheckedAndIndeterminateNodes()
    // },
    TreeChange () {
      let data1 = this.$refs.tree.getCheckedAndIndeterminateNodes()
      console.log('this.dataPermissData')
      console.log(this.dataPermissData)
      let list1=[]
      data1.forEach(v=>{
        if(v.parentId==0){
          list1.push(v)
        }
      })
      this.dataPermissData = list1
    },
    async handleSubmit () {
      const ifVlid = await this.$refs.form.validate()
      if (ifVlid && this.editType === 'create') {
        this.handleCreate()
      }
      if (ifVlid && this.editType === 'update') {
        this.handleUpdate()
      }
    },

    async handleCreate () {
      console.log('this.editItem',this.editItem)
      let data=
      {
        parentId:this.editItem.id,
        // parentId:this.editItem.parentId==0?this.editItem.id:this.editItem.parentId,
        areaName:this.formData.areaName,
        areaType:this.formData.areaType==1?'D':'G'
      }
      const { success } = await addArea(data)
      if (success) {
        this.$Message.success(this.$t('devicesCreate.devCreateS'))
        this.ifUpdateUpper = true
        this.$_modal_close()
      } else {
        this.$Message.error(this.$t('newDevices.createdSiteTip'))
      }
    },
    async handleUpdate () {
      // const formData = _.cloneDeep(this.formData)
      let data=
        {
            parentId: this.editItem.parentId,
            areaName: this.formData.areaName,
            id:this.editItem.id
        }
      const { success } = await updateArea(data)
      if (success) {
        this.$Message.success(this.$t('devicesIndex.devdevStrUpdateSucc'))
        this.ifUpdateUpper = true
        this.$_modal_close()
      } else {
        this.$Message.error(this.$t('devicesIndex.devdevStrUpdatefailed'))
      }
    },
    handleChange(value){

    },
    handleChange1 (value) {
      // Change时候赋值给recordType  然后关闭级联下拉框可以选中
      this.recordType = value
      // console.log('woshi====',this.recordType)
    },
    // 获取权限组
    // async getRole () {
    //   let areaId = {
    //     page: 1,
    //     limit: 100,
    //     username: '',
    //   }
    //   const { success, data } = await getList(areaId)
    //   if (success) {
    //     // console.log(data.data)
    //     this.roleCheck = data.data
    //   }
      
    // },
    // 获取当前场所项管理员信息
    async getAreaItem1 () {
      console.log('getAreaItem1')
      console.log(this.editItem)
      const { success, data } = await getAreaItem({id: this.editItem.id})
      if (success) {
        console.log('getAreaItem')
        console.log(data.data)
        // this.roleCheck = data.data
      }
      
    },
    // 获取上级场所
    async getParent(){
      console.log('getParentAreas1')
      let data1={
        id:this.editItem.id,
        parentId: this.editItem.parentId,
      }
      const { success ,data} = await getParentArea(data1)
      if (success) {
        setTimeout(() => {
          this.recordType=data.data
        }, 500);
        
      } else {
        this.$Message.error(this.$t('staffIndex.stafferror'))
      }
    },
    async getSiteList(){
       const { success ,data} = await getSite()
      if (success) {
        const list=data.data
        let listString = JSON.stringify(list).replace(/"id"/g, '"value"')
        let listStringLabel = listString.replace(/"areaName"/g, '"label"')
        let listStringChildren = listStringLabel.replace(/"children"/g, '"children"')
        var aParse = JSON.parse(listStringChildren)
        function dG(params) {
          params.forEach(v=>{
            if(v.children){
              dG(v.children)
            }else{
              v.children=[]
            }
          })
        }
        dG(aParse)
        if(aParse&&aParse[0].label==="新场所"){
          let arr11=aParse
          arr11[0].label=this.$t('title.newSite')
          this.recordTypes=arr11
        }else{
          this.recordTypes=aParse 
        }
        // this.recordTypes=aParse
        // if(this.editType != 'update'){
          
        //   setTimeout(()=>{
        //     let arr=[]
        //     arr.push(this.recordTypes[0].value)
        //     this.recordType=arr     
        //   },500)
        // }
      } else {
        this.$Message.error(this.$t('staffIndex.stafferror'))
      }
    },
    //设置管理员提交
    async settIngAdmin () {
      
      // this.handleSubmit1('formDynamic')
      if(!this.dataPermissData[0]){
        this.$Message.error(this.$t('permissionsIndex.roleSelper'))
      }
      var list=[]
      for (let index = 0; index < this.formDynamic.items.length; index++) {
        const v = this.formDynamic.items[index];
        if(v.status==1){
          let reg=/^[0-9]{6,12}$/
          if(!v.value.username||!v.value.password||v.value.password.length<6||!v.value.passwordSure||v.value.passwordSure!=v.value.password){
            console.log('用戶名項校驗不通過')
            return
          }else if(reg.test(v.value.password)==false){
            this.$Message.error(this.$t('loginIndex.loginpwdCheck9'))
            return
          }else{
            v.value.username = v.value.username.replace(/\s*/g,"");
          }
          let user
          if(this.dateTime==0){
            user={
              username:v.value.username,
              password:v.value.password,
              dateType:0,
            }
          }else{
            //有效期时间处理
            var startDate = ""
            var endDate = ""
            if (this.selectDate.length) {
              [startDate, endDate] = this.selectDate
              if (moment.isDate(startDate)) {
                startDate = moment(startDate).format('YYYY-MM-DD')
              }
              if (moment.isDate(endDate)) {
                endDate = moment(endDate).format('YYYY-MM-DD')
              }
            }
            let period=startDate+'~'+endDate
            user={
              username:v.value.username,
              password:v.value.password,
              dateType:1,
              period:period
            }
          }
          list.push(user)         
        }
      }
      let language = localStorage.getItem('language') || 'zh'
      switch (language) {
        case 'zh':
          language = 1
          break
        case 'zf':
          language = 2
          break
        case 'en':
          language = 3
          break
        case 'ja':
          language = 4
          break
        case 'ko':
          language = 5
          break
      }
      let data1={
        userList:list,
        areaName:this.editItem.areaName,
        id:this.editItem.id,
        companyId:this.editItem.companyId,
        role:{roleMenu:JSON.stringify(this.dataPermissData)},
        language: language,
        // role:JSON.stringify(this.dataPermissData),
      }
      
      // this.formData.user = list.join(',') || ''
      const { success, data } = await addAreaAdmin(data1)
      if (success) {
        this.$Message.success(this.$t('devicesCreate.devCreateUpdateS'))
        // this.show = false
        this.ifUpdateUpper = true
      } else {
        // console.log(data)
        if(data=="权限组已存在"){
          this.$Message.error(this.$t('newDevices.createdSiteTip3'))
        }else if(data=="用户名已存在"){
          this.$Message.error(this.$t('newDevices.createdAdminTip2'))
        }
      }
      this.$_modal_close()
    },
    // 动态新增接收邮箱方法
    handleSubmit1 (name) {
        this.$refs.formDynamic.validate((valid) => {
            if (valid) {
                // this.$Message.success('Success!');
              console.log('用戶名校驗通過')
              return
            } else {
                this.$Message.error(this.$t('loginIndex.loginpwdCheck8'));
                this.handleReset('formDynamic')
                return
            }
            
        })
    },
    handleReset (name) {
        this.$refs.formDynamic.resetFields();
    },
    handleAdd () {
        this.index++;
        this.formDynamic.items.push({
            value: {
              username:'',
              password:'',
              passwordSure:'',
            },//名字
            index: this.index,
            status: 1
        });
    },
    handleRemove (index) {
      if(this.formDynamic.items.length>1){
        let count=0;
        this.formDynamic.items.forEach(v=>{
          if(v.status==1){
            count++
          } 
        })
        if(count>1){
          this.formDynamic.items[index].status = 0;
        }else{
          this.$Message.error(this.$t('permissionsIndex.UserName')+this.$t('emailSetting.Noempty'));
        }
      }else{
        this.$Message.error(this.$t('permissionsIndex.UserName')+this.$t('emailSetting.Noempty'));
      }  
    }
  },
}
</script>
